//
//  DYZDetailedInfoController.m
//  DYZCars
//
//  Created by 李凯 on 16/8/20.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZDetailedInfoController.h"
#import "DSHTTPClient.h"
#import "DYZCarShowModel.h"
#import "DYZDetailedInfoCell.h"
#import "DYZBottomCell.h"
#import "UIColor+Addition.h"
#import "DYZNormalCell.h"

@interface DYZDetailedInfoController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *detailedInfo;
@property (nonatomic, strong) UITableView *tableView;

@end

static NSString *normalCellid = @"normalCellid";
static NSString *firstCellid = @"firstCellid";
static NSString *bottomCellid = @"bottomCellid";

@implementation DYZDetailedInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
    [self setupUI];
    
}

- (void)setupUI
{
    UITableView *tabelView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    tabelView.estimatedRowHeight = 100;
    tabelView.rowHeight = UITableViewAutomaticDimension;
    tabelView.dataSource = self;
    tabelView.delegate = self;
    [tabelView registerNib:[UINib nibWithNibName:@"detailedInfo" bundle:nil] forCellReuseIdentifier:firstCellid];
    [tabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:normalCellid];
    [tabelView registerNib:[UINib nibWithNibName:@"bottomCell" bundle:nil] forCellReuseIdentifier:bottomCellid];
    [self.view addSubview:tabelView];
    self.tableView = tabelView;
}

- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"1" forKey:@"type"];
    
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/searchlist.json.php" withParam:param withSuccessBlock:^(id data) {
        NSDictionary *dict = [data objectForKey:@"data"];
        NSArray *arr = [NSArray yy_modelArrayWithClass:[DYZCarShowModel class] json:[dict objectForKey:@"carList"]];
//        NSLog(@"%@", arr);
        self.detailedInfo = arr.copy;
        [self.tableView reloadData];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return self.detailedInfo.count;
    }
    else
    {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            DYZDetailedInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:firstCellid forIndexPath:indexPath];
            cell.model = self.detailedInfo[indexPath.row];
            return cell;
        }
        else
        {
            DYZNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:normalCellid forIndexPath:indexPath];
            DYZCarShowModel *model = self.detailedInfo[indexPath.row];
            cell.textLabel.text = model.remark;
//            cell.textLabel.text = @(indexPath.row).description;
            return cell;
        }
    }
    else
    {
        DYZBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:bottomCellid forIndexPath:indexPath];
        [cell.collectBtn addTarget:self action:@selector(collectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.callBtn addTarget:self action:@selector(callBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
}

//添加collectBtn的点击事件
- (void)collectBtnClick: (UIButton *)sender
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"知道啦" otherButtonTitles:nil];
    [alertView show];
}

//添加callBtn的点击事件
- (void)callBtnClick: (UIButton *)sender
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"张峰" message:@"13485724629" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //title
    NSMutableAttributedString *alertTitleStr = [[NSMutableAttributedString alloc] initWithString:@"张峰"];
    [alertTitleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 2)];

    [alertController setValue:alertTitleStr forKey:@"attributedTitle"];
    
    //message
    NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"13485724629"];
    [alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, 11)];

    [alertController setValue:alertMessageStr forKey:@"attributedMessage"];
    
    UIAlertAction* action1 = [UIAlertAction actionWithTitle:@"拨打" style:UIAlertActionStyleCancel handler:^(UIAlertAction* _Nonnull action) {
        
    }];
    UIAlertAction* action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* _Nonnull action) {
        
    }];
   
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self presentViewController:alertController animated:YES completion:nil];
}



#pragma mark - UITabelViewDelegate
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0)
    {
        return nil;
    }
    else
    {
        return @"联系人";
    }
   
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 1)
    {
        UIView *headerView = [[UIView alloc]init];
        headerView.backgroundColor = [UIColor colorWithHex:0xf0f0f0];
        UILabel *headerLabel = [[UILabel alloc]init];
        headerLabel.text = @"联系人";
        headerLabel.font = [UIFont systemFontOfSize:12];
        [headerLabel sizeToFit];
        [headerView addSubview:headerLabel];
        [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(headerView);
            make.left.offset(10);
        }];
        return headerView;
    }
    else
    {
        return nil;
    }
}




@end
